package com.elitetyc.customview.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.PathEffect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.text.LoginFilter;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.LogUtils;
import com.elitetyc.customview.R;

/**
 * Created by elitetyc on 2019/6/6.
 * Describe:
 */
public class LuDaShiView extends View {

//    旋转的圆的大小
    private float outDotSize;
//    虚线的宽度
    private float dottedWidth;
//    实线的宽度
    private float solidLineWidth;
//    内部实线的宽度
    private float inSolidLineWidth;
//    半径
    private float radius;
//    刻度线的长度
    private float graduateWidth;

//    当前进度 最大100
    private int currentNum=0;

    Paint mpaint,mpaint2,mpaint3,mpaint4;

//    旋转的点有多少个
    private int dotSize = 20;
//    刻度线条数
    private int indicatorSize = 80;



    @Override
    public String toString() {
        return "LuDaShiView{" +
                "outDotSize=" + outDotSize +
                ", dottedWidth=" + dottedWidth +
                ", solidLineWidth=" + solidLineWidth +
                ", inSolidLineWidth=" + inSolidLineWidth +
                ", radius=" + radius +
                ", graduateWidth=" + graduateWidth +
                ", mpaint=" + mpaint +
                '}';
    }

    public LuDaShiView(Context context) {
        this(context,null);
    }

    public LuDaShiView(Context context,  AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LuDaShiView(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LuDaShiView);
        outDotSize = array.getDimension(R.styleable.LuDaShiView_outDotSize,ConvertUtils.dp2px(3));
        dottedWidth = array.getDimension(R.styleable.LuDaShiView_dottedWidth,ConvertUtils.dp2px(1));
        solidLineWidth = array.getDimension(R.styleable.LuDaShiView_solidLineWidth,ConvertUtils.dp2px(1));
        radius = array.getDimension(R.styleable.LuDaShiView_lRadius,ConvertUtils.dp2px(100));
        graduateWidth = array.getDimension(R.styleable.LuDaShiView_graduateWidth,ConvertUtils.dp2px(15));
        inSolidLineWidth = dottedWidth*3;
        array.recycle();
        mpaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mpaint.setDither(true);
        mpaint.setColor(Color.WHITE);
        mpaint.setStyle(Paint.Style.STROKE);
        mpaint.setAlpha(0x40);

        mpaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
        mpaint2.setDither(true);
        mpaint2.setColor(Color.WHITE);
        mpaint2.setStyle(Paint.Style.STROKE);
        mpaint2.setAlpha(0xCC);

        mpaint4 = new Paint(Paint.ANTI_ALIAS_FLAG);
        mpaint4.setDither(true);
        mpaint4.setColor(0x33ffffff);
        mpaint4.setStyle(Paint.Style.STROKE);

        mpaint3 = new Paint(Paint.ANTI_ALIAS_FLAG);
        mpaint3.setDither(true);
        mpaint3.setColor(0xffffffff);
        mpaint3.setStyle(Paint.Style.FILL);


        Log.e("tyc",toString());


    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSzie = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSzie = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);


        if (widthMode!=MeasureSpec.EXACTLY){
            widthSzie = ConvertUtils.dp2px(300);
        }
        if (heightMode != MeasureSpec.EXACTLY){
            heightSzie = ConvertUtils.dp2px(300);
        }
        
        if (widthSzie>heightSzie){
            widthSzie=heightSzie ;
        }else {
            heightSzie=widthSzie;
        }
        setMeasuredDimension(widthSzie,heightSzie);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        canvas.translate(getWidth()/2,getHeight()/2);
        drawDottedLine(canvas);
        drawIndicator(canvas);
        drawOutDot(canvas);
        canvas.restore();
    }

    private void drawOutDot(Canvas canvas) {
//        计算出每个点相隔的距离,假设有20个点
        canvas.save();
        float angle = 360/dotSize;
        for (int i=0;i<dotSize;i++){
            mpaint3.setMaskFilter(new BlurMaskFilter(outDotSize/2,BlurMaskFilter.Blur.SOLID));
            canvas.drawCircle(0,-radius-ConvertUtils.dp2px(15),outDotSize/2,mpaint3);
            canvas.rotate(angle);
        }
        canvas.restore();



//        Canvas canvas1 = new Canvas();
//        canvas1.save();
////        canvas1.translate(getWidth()/2,getHeight()/2);
//        float angle = 360/dotSize;
//        for (int i=0;i<dotSize;i++){
//            mpaint3.setMaskFilter(new BlurMaskFilter(outDotSize/2,BlurMaskFilter.Blur.SOLID));
//            canvas1.drawCircle(0,-radius-ConvertUtils.dp2px(15),outDotSize/2,mpaint3);
//            canvas1.rotate(angle);
//        }
//        canvas1.restore();

    }

    private void drawIndicator(Canvas canvas) {
        canvas.save();
        int sweep;
        if (currentNum>100){
            sweep = 360;
        }else {
            sweep = (int) ((float)currentNum/(float)100*360);
        }
        mpaint2.setStrokeWidth(inSolidLineWidth);
        RectF rectF = new RectF(-radius,-radius,radius,radius);
        canvas.drawArc(rectF,-90,sweep,false,mpaint2);

        mpaint4.setStrokeWidth(solidLineWidth);
        int w = ConvertUtils.dp2px(6);
        RectF rectF2 = new RectF(-radius-w,-radius-w,radius+w,radius+w);
        canvas.drawArc(rectF2,-90,sweep,false,mpaint4);

//        画刻度线
        canvas.save();
        int angle =0;
        int smallAngle = 360/indicatorSize;
        do {
            canvas.drawLine(0,-radius+inSolidLineWidth/2,0,-radius+inSolidLineWidth/2+graduateWidth,mpaint4);
            canvas.rotate(smallAngle);
            angle+=smallAngle;
        }while (angle<=sweep);

        canvas.restore();

//      画亮点
        float x = (float)((radius)*Math.cos(Math.toRadians(-90+sweep)));
        float y = (float)((radius)*Math.sin(Math.toRadians(-90+sweep)));
        mpaint3.setMaskFilter(new BlurMaskFilter(inSolidLineWidth+2,BlurMaskFilter.Blur.SOLID));
        canvas.drawCircle(x,y,inSolidLineWidth+2,mpaint3);


        canvas.restore();


    }

    private void drawDottedLine(Canvas canvas) {
        canvas.save();
        PathEffect dashPathEffect = new DashPathEffect(new float[]{10,10},0);
        mpaint.setStrokeWidth(dottedWidth);
        mpaint.setPathEffect(dashPathEffect);
        // 虚线圆
        canvas.drawCircle(0,0,radius,mpaint);
        canvas.restore();

    }

    public void setCurrentNum(int currentNum) {
        this.currentNum = currentNum;
        invalidate();
    }
}
